#include <iostream>
#include <fstream>
#include <string>
#include "heapUtilities.h"

using namespace std;

int main(int argc, char **argv) {
	if (argc != 2) {
		cout << "Usage: " << argv[1] << " input_file." << endl;
		return 0;
	}

	char file_name[256];
	sprintf(file_name, "%s", argv[1]);

	fstream input_file;
	input_file.open(file_name, fstream::in);
	if (!input_file.is_open()) {
		cout << "Input file " << file_name << " open error!" << endl;
		return 1;
	}

	string str;
	int *array;
	unsigned node_num = 0;

	// assume max node number is 1000
	array = (int *) malloc(sizeof(int) * 1000);
	if (array == NULL) {
		cout << "Allocate array error, NULL!" << endl;
		return 1;
	}

	while (input_file >> str) {
		array[node_num++] = atoi(str.c_str());
	}
	input_file.close();

	unsigned int i;
	createHeap(array, node_num);
	cout << "Before sort: " << endl;
	for (i = 0; i < node_num; i++) {
		cout << array[i] << endl;
	}
	cout << endl;

	heapSort(array, node_num);

	cout << "After sort: " << endl;
	for (i = 0; i < node_num; i++) {
		cout << array[i] << endl;
	}

	return 0;
}
		
